/**
 * Created by FDG282 on 2017/10/10.
 */
$(function(){

    ajaxShow();  //执行分页
    
    //banner视频
    $('.video-list-content .video-list-banner-div').find('.video-list-icon').on('click',function(){
        $(this).hide().siblings('img').hide().siblings('#video-list-banner').removeClass('hide');
        $('.videos_list_box').find('li').find('.video-list-icon').show().siblings('img').show().siblings('video').addClass('hide');
        var videos_list_box_li = $('.videos_list_box').find('li').find('video');
        $(videos_list_box_li).each(function(){
            this.pause();
        })
        var that = $(this);
        videojs('video-list-banner', {
            "techOrder": ["html5"]
        },function(){
            var myPlayer = this;
            myPlayer.play();
            myPlayer.on("ended",function(){
                $('#video-list-banner').addClass('hide').siblings('img').show().siblings('.video-list-icon').show();
            })
        })
        
    })
    
    // mod FDG416 2018/04/17 ID586 【商城需求】视频列表页展示调整 end

});


function ajaxShow() {
    var paging = 1;//页码数
    // dropload函数接口设置
    $('#videos_list_box').dropload({
        scrollArea: window,
        // 下拉刷新模块显示内容
        //  上拉加载更多 回调函数
        loadDownFn: function (me) {
            //var url = requeryUrl;
            $.ajax({
                type: 'GET',
                url: jsonUrl+'?page='+paging,
                dataType: 'json',
                success: function (result) {
                    // 选择需要显示的数据 拼接 DOM
                    if(result.length==0){
                        me.lock();
                        me.noData();
                        me.resetload();
                    }else {
                        var html = '';
                        for (var i = 0; i < result.length; i++) {
                            html +='<li>';
                            html +='<div class="pr">';
                            html +='<span class="video-list-icon"></span>';
                            html +='<img src="'+imgUrl+result[i].article_pic+'" alt="'+result[i].article_title+'" >';
                            html +='<video src="'+result[i].video_url_pc+'" id="video-list-item'+result[i].id+'" class="video-js hide video"  controls preload="none" poster="">';
                            html +='</video>';
                            html +='</div>';
                            html +='<a href="javascript:;">';
                            html +='<div class="news-ul-text">';

                            if(result[i].articleIdentification){
                                html +='<p class="news-ul-t-p01">'+result[i].articleIdentification.name+'</p>';
                            }else{
                                html +='<p class="news-ul-t-p01"> </p>';
                            }
                            html +='<p class="news-ul-t-p02">'+result[i].article_title+'</p>';
                            html +='<p class="news-ul-t-p03">'+result[i].summary+'</p>';
                            html +='</div>';
                            html +='</a>';
                            html +='</li>';
                        }

                        setTimeout(function () {
                            $('.scroll_news').append(html);
                            //视频列表
                            var _arr = [];
                            $('.videos_list_box').find('li').each(function(){
                                var id_v = $(this).find('video').attr('id');
                                _arr.push(id_v);
                                $(this).children('.pr').find('.video-list-icon').click(function () {
                                    videojs('video-list-banner', {
                                        "techOrder": ["html5"]
                                    },function(){
                                        var myPlayer = this;
                                        myPlayer.pause();
                                        
                                    })
                                    var that = $(this);
                                    $('#video-list-banner').addClass('hide').siblings('img').show().siblings('.video-list-icon').show();
                                    $(this).hide().siblings('img').hide().siblings('.video').removeClass('hide');
                                    $(this).parents('li').siblings().find('.video-list-icon').show().siblings('img').show().siblings('video').addClass('hide');
                                    var id = $(this).siblings('video').attr('id');
                                    for(var i = 0; i < _arr.length; i ++) {
                                        if(_arr[i] == id) {
                                            _play(id);

                                        }else {
                                            _stop(_arr[i]);
                                        }
                                    }

                                    var myPlay = $('#'+id);
                                    myPlay.on("ended",function(){
                                        $(that).show().siblings('img').show().siblings('video').addClass('hide');
                                    })
                                })
                            });
                            me.resetload();
                        }, 1000);
                        me.resetload();
                    }
                },
                error: function (xhr, type) {
                    console.log('加载出错了!');
                    me.resetload();
                }
            });

            paging++; // 每次请求，页码加1
        },
        threshold:200,
        domDown: {
            domClass: 'dropload-down',
            // 滑动到底部显示内容
            domRefresh: '<div class="dropload-refresh"></div>',
            // 内容加载过程中显示内容
            domLoad: '<div class="dropload-load"><span class="loading"></span>加载中...</div>',
            // 没有更多内容-显示提示
            domNoData: '<div class="dropload-noData">暂无数据</div>'
        }
    });
}

// mod FDG416 2018/04/17 ID584 【商城需求】文章列表页展示调整 start
//判断是否IE/火狐浏览器
_isIE();
function _isIE() {
    var news_banner_t_p02 = $('.news-banner-t-p02').text().length;
    var news_banner_t_p03 = $('.news-banner-t-p03').text().length;
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
    var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1;
    var isH = userAgent.indexOf('Firefox') > -1;
    $('.scroll_news').children().each(function(){
        var news_ul_t_p02 = $(this).find('.news-ul-t-p02').text().length;
        var news_ul_t_p03 = $(this).find('.news-ul-t-p03').text().length;
        if(isIE || isH) {
            if(news_banner_t_p02 > 52) {
                $('.news-banner-t-p02').text($('.news-banner-t-p02').text().slice(0,52)+'...');
            }
            if(news_banner_t_p03 > 140) {
                $('.news-banner-t-p03').text($('.news-banner-t-p03').text().slice(0,140)+'...');
            }
            if(news_ul_t_p02 > 25) {
                $(this).find('.news-ul-t-p02').text($(this).find('.news-ul-t-p02').text().slice(0,25)+'...');
            }
            if(news_ul_t_p03 > 65) {
                $(this).find('.news-ul-t-p03').text($(this).find('.news-ul-t-p03').text().slice(0,65)+'...');
            }
        }
    })
}

function _play(id) {
    document.getElementById(id).play();
}
function _stop(id) {
    document.getElementById(id).pause();
}